#include <cstdio>
#include <list>

#include "Player.h"
#include "DataDefine.h"

Card* getCard(int id);

int copyDeck(list<Card*> src, list<Card*> &dst)
{
	dst.clear();
	for (Card* c : src)
	{
		dst.push_back(getCard(c->id));
	}
	return 0;
}

int deckSort(list<Card*>& dst)
{
	dst.sort([](Card* a, Card* b)->bool{
		if (a->cost != b->cost)
		{
			return a->cost < b->cost;
		}
		return a->id < b->id;
	});
	return 0;
}

int readDeck(list<Card*> &deck, string path)
{
	FILE* filein;
	fopen_s(&filein, path.c_str(), "r");
	if (!filein)
	{
		printf("deck read failed.\n");
		return 0;
	}
	deck.clear();
	int id;
	for (int i = 0; i < DECK_NUM; i++)
	{
		fscanf_s(filein, "%d", &id);
		deck.push_back(getCard(id));
	}
	fclose(filein);

	return 0;
}

int writeDeck(list<Card*>& deck, string path)
{
	FILE* fileout;
	fopen_s(&fileout, path.c_str(), "w");
	if (!fileout)
	{
		printf("deck write failed.\n");
		return 0;
	}
	for (Card* c : deck)
	{
		fprintf(fileout, "%d\n", c->id);
	}
	fclose(fileout);

	return 0;
}